// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Drawing;

namespace RealWorld.Grids
{
    public class TwoHeadedGridView : GridView
    {
        public string HeaderText
        {
            get
            {
                object val = this.ViewState["HeaderText"];
                if (null == val)
                {
                    return String.Empty;
                }

                return (String)val;
            }
            set
            {
                this.ViewState["HeaderText"] = value;
            }
        }

        protected Table InnerTable
        {
            get
            {
                if (this.HasControls())
                {
                    return (Table)this.Controls[0];
                }

                return null;
            }
        }

        protected override void OnDataBound(EventArgs e)
        {
            base.OnDataBound(e);

            this.CreateSecondHeader();
        }

        private void CreateSecondHeader()
        {
            GridViewRow row = new GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Normal);
            TableCell th = new TableHeaderCell();

            th.HorizontalAlign = HorizontalAlign.Center;
            th.ColumnSpan = this.Columns.Count;
            th.BackColor = Color.SteelBlue;
            th.ForeColor = Color.White;
            th.Font.Bold = true;
            th.Text = this.HeaderText;
            row.Cells.Add(th);

            this.InnerTable.Rows.AddAt(0, row);
        }
    }
}
